#import "DictPageViewPlugin.h"
#import "DictPageViewFactory.h"
#import "Common.h"
#import "DictPageViewPlatformView.h"

static FlutterMethodChannel* s_channel = NULL;

@implementation DictPageViewPlugin

+(FlutterMethodChannel*)getChannel {
    return s_channel;
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    s_channel = [FlutterMethodChannel
      methodChannelWithName:@"dict_page_view_plugin"
            binaryMessenger:[registrar messenger]];
  DictPageViewPlugin* instance = [[DictPageViewPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:s_channel];

  DictPageViewFactory* fc = [[DictPageViewFactory alloc] initWith:registrar];
  [registrar registerViewFactory:fc withId:@"com.maiyuren.sudict/dictpageview"];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    NSLog(@"%@.. %@", call.method, call.arguments ? call.arguments : @"");

  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  }
  else if ([@"loadTabs" isEqualToString:call.method]) {
    NSDictionary* args = [Common objectWithJsonString:call.arguments];
    NSNumber* id = [args objectForKey:@"id"];
    DictPageViewPlatformView* platformView = [DictPageViewPlatformView findViewByViewIdentifier:[id intValue]];
    if (platformView) {
      [platformView loadTab:[args objectForKey:@"info"]];
    }
    result(nil);
  }
  else if ([@"changeTab" isEqualToString:call.method]) {
      NSDictionary* args = [Common objectWithJsonString:call.arguments];
      NSNumber* id = [args objectForKey:@"id"];
      DictPageViewPlatformView* platformView = [DictPageViewPlatformView findViewByViewIdentifier:[id intValue]];
      if (platformView) {
          NSNumber* tabIndex = [args objectForKey:@"tabIndex"];
          [platformView changeTab:[tabIndex intValue]];
      }
      result(nil);
  }
  else if ([@"loadUrl" isEqualToString:call.method]) {
      NSDictionary* args = [Common objectWithJsonString:call.arguments];
      NSNumber* id = [args objectForKey:@"id"];
      DictPageViewPlatformView* platformView = [DictPageViewPlatformView findViewByViewIdentifier:[id intValue]];
      if (platformView) {
          NSNumber* tabIndex = [args objectForKey:@"tabIndex"];
          NSString* url = [args objectForKey:@"url"];
          [platformView loadUrl:url atIndex:[tabIndex intValue]];
      }
      result(nil);
  }
  else if ([@"takePhoto" isEqualToString:call.method]) {
      NSDictionary* args = [Common objectWithJsonString:call.arguments];
      NSNumber* id = [args objectForKey:@"id"];
      DictPageViewPlatformView* platformView = [DictPageViewPlatformView findViewByViewIdentifier:[id intValue]];
      if (platformView) {
          NSNumber* tabIndex = [args objectForKey:@"tabIndex"];
          [platformView takePhoto:[tabIndex intValue]];
      }
      result(nil);
  }
  else {
    result(FlutterMethodNotImplemented);
  }
}

@end
